package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;

import java.util.List;

/**
 * 课程内容管理 service层实现类
 */
public class CourseContentServiceImpl implements CourseContentService {

    CourseContentDao contentDao = new CourseContentDaoImpl();

    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        return contentDao.findSectionAndLessonByCourseId(courseId);
    }

    @Override
    public Course findCourseByCourseId(int courseId) {
        return contentDao.findCourseByCourseId(courseId);
    }

    @Override
    public String saveSection(Course_Section section) {

        // 1.补全章节信息
        section.setStatus(2);//状态 0 隐藏，1 待更新，2 已发布
        String date = DateUtils.getDateFormart();
        section.setCreate_time(date);
        section.setUpdate_time(date);

        // 2.调用dao
        int row = contentDao.saveSection(section);

        // 3.根据是否插入成功，封装对于信息
        String result;
        if (row > 0) {
            // 保存成功
            result = StatusCode.SUCCESS.toString();
        } else  {
            result = StatusCode.FAIL.toString();
        }
        return result;

    }

    @Override
    public String updateSection(Course_Section section) {

        // 1.补全信息
        section.setUpdate_time(DateUtils.getDateFormart());

        // 2.调用dao
        int row = contentDao.updateSection(section);

        // 3.判断是否插入成功
        String result;
        if (row > 0) {
            result = StatusCode.SUCCESS.toString();
        } else {
            result = StatusCode.FAIL.toString();
        }
        return result;

    }

    @Override
    public String updateSectionStatus(int id, int status) {

        int row = contentDao.updateSectionStatus(id, status);

        // 判断是否修改成功
        String result;
        if (row > 0) {
            // 修改状态成功
            result = StatusCode.SUCCESS.toString();
        } else {
            // 修改失败
            result = StatusCode.FAIL.toString();
        }
        return result;

    }

    @Override
    public String saveLesson(Course_Lesson lesson) {

        // 1.补全信息
        lesson.setStatus(2); // 课时状态,0-隐藏，1-未发布，2-已发布
        String date = DateUtils.getDateFormart();
        lesson.setCreate_time(date);
        lesson.setUpdate_time(date);

        // 2.调用dao
        int row = contentDao.saveLesson(lesson);

        // 3.判断是否保存成功
        String result;
        if (row > 0) {
            // 保存成功
            result = StatusCode.SUCCESS.toString();
        } else {
            // 保存失败
            result = StatusCode.FAIL.toString();
        }
        return result;

    }

    @Override
    public String updateLesson(Course_Lesson lesson) {

        // 1.补全信息
        lesson.setUpdate_time(DateUtils.getDateFormart());

        // 2.调用dao
        int row = contentDao.updateLesson(lesson);

        // 3.判断是否修改成功
        String result;
        if (row > 0) {
            // 修改成功
            result = StatusCode.SUCCESS.toString();
        } else {
            result = StatusCode.FAIL.toString();
        }
        return result;

    }


}
